﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.App;
using Domain.Types;
using FacadeApi.Extension;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace FacadeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class UserPropController : ControllerBase
    {
        public UserPropApplication userPropApplication { get; set; }
        public ILogger<UserPropController> _logger { get; set; }

        /// <summary>
        /// 获取用户道具信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetUserProp()
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = userPropApplication.GetUserProp(int.Parse(uid));

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
        /// <summary>
        /// 道具价格列表  刷新=0
        /// </summary>
        /// <param name="propType"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetPropList(PropType propType)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = userPropApplication.GetPropList(int.Parse(uid), propType);

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
        /// <summary>
        /// 购买用户道具
        /// </summary>
        /// <param name="propType">刷新=0</param>
        /// <param name="Num">BuySuccess = false 需要冲值金额</param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult PayUserProp([FromForm]PropType propType, [FromForm] int Num)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;

                var r = userPropApplication.PayUserProp(int.Parse(uid), propType, Num);

                if (r==0)
                {
                    return this.Success(new { BuySuccess = true, PayMoney = 0 });
                }
                else
                {
                    return this.Success(new { BuySuccess = false, PayMoney = r });
                }                
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 使用道具
        /// </summary>
        /// <param name="propType">刷新=0</param>
        /// <param name="TaskID">任务ID</param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult UseUserPropForTask([FromForm] PropType propType, [FromForm] int TaskID)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;

                var r = userPropApplication.UseUserPropForTask(int.Parse(uid), propType, TaskID);

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }




        /// <summary>
        /// 获取推荐道具信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetRecommendPropInfo()
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = userPropApplication.GetRecommendPropInfo(int.Parse(uid));

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 支付并使用推荐道具
        /// </summary>
        /// <param name="hour">小时</param>
        /// <param name="taskID">任务</param>
        /// <returns>PaySuccess==false 需要冲值费用</returns>
        [HttpPost, TokenValidFilter]
        public ActionResult PayRecommendPropForTask([FromForm] int hour, [FromForm] int taskID)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = userPropApplication.PayRecommendPropForTask(int.Parse(uid),hour,taskID);

                if (r>0)
                {
                    return this.Success(new { PaySuccess = false, PayMoney = r });
                }
                return this.Success(new { PaySuccess = true, PayMoney = 0 });
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

    }
}
